﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using Ccit.ClassLibrary;

namespace Ccit.Utils
{
    /// <summary>
    /// 页面类
    /// </summary>
    public class Page : System.Web.UI.Page
    {
        private const string _cssFolder = "css/";
        private string _cssFile = "standard";
        private const string _cssSuffix = ".css";  //样式表扩展名

        private const string _scriptFolder = "script/";
        private const string _scriptSuffix = ".js";  //脚本资源文件扩展名
        private string _relativelyPath = "";

        /// <summary>
        /// Page 输出
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            _relativelyPath = RequestContext.GetRelativelyPath();  //获取相对路径
            _cssFile = getSysApplyStyle();  //获取系统当前应用样式文件
            string cssFile = _cssFolder + _cssFile + _cssSuffix;

            string scriptFile = _scriptFolder + "jbase64" + _scriptSuffix;

            writer.AddAttribute("http-equiv", "pragma");
            writer.AddAttribute(HtmlTextWriterAttribute.Content, "no-cache");
            writer.RenderBeginTag(HtmlTextWriterTag.Meta);
            writer.RenderEndTag();

            writer.AddAttribute("http-equiv", "Cache-Control");
            writer.AddAttribute(HtmlTextWriterAttribute.Content, "no-cache,must-revalidate");
            writer.RenderBeginTag(HtmlTextWriterTag.Meta);
            writer.RenderEndTag();

            writer.AddAttribute("http-equiv", "Content-Type");
            writer.AddAttribute(HtmlTextWriterAttribute.Content, "text/html; charset=gb2312");
            writer.RenderBeginTag(HtmlTextWriterTag.Meta);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Rel,"Stylesheet");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, _relativelyPath + cssFile);
            writer.AddAttribute(HtmlTextWriterAttribute.Type,"text/css");
            writer.RenderBeginTag(HtmlTextWriterTag.Link);
            writer.RenderEndTag();
                        
            writer.AddAttribute(HtmlTextWriterAttribute.Src, _relativelyPath + scriptFile);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.RenderEndTag();

            //waitLoad(ref writer);
            
            base.Render(writer);
        }

        private string getSysApplyStyle()
        {            
            return ConfigLib.GetAttribute(eNodeType.CSS);
        }

        #region logout code
        //private void waitLoad(ref System.Web.UI.HtmlTextWriter writer)
        //{
        //    writer.AddAttribute("id", "divLoading");
        //    writer.AddStyleAttribute("position", "absolute");
        //    writer.RenderBeginTag(HtmlTextWriterTag.Div);
        //    writer.AddAttribute("src", _relativelyPath + "images/loading.gif");
        //    writer.RenderBeginTag(HtmlTextWriterTag.Img);
        //    writer.RenderEndTag();
        //    writer.Write("Loading...");
        //    writer.RenderEndTag();

        //    string strScript = "function setposition() {document.all.divLoading.style.top=document.body.offsetHeight/2;";
        //    strScript += "document.all.divLoading.style.left=document.body.offsetWidth/2; }";
        //    strScript += "setposition();";
        //    writer.AddAttribute("type","text/javascript");
        //    writer.RenderBeginTag(HtmlTextWriterTag.Script);
        //    writer.Write(strScript);
        //    writer.RenderEndTag();
        //}
        #endregion

        /// <summary>
        /// page 初始化前事件操作
        /// </summary>
        /// <param name="e"></param>
        protected override void  OnPreInit(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string relationPath = RequestContext.GetRelativelyPath();
                string imgPath = relationPath + "images/loading.gif";
                Response.Charset = "utf-8";
                string loadScript = "<div id='divLoading' style='position:absolute;'>";
                loadScript += "<img src='" + imgPath + "'><font font-family='times new roman' size='2'><strong>Loading...</strong></font></div> ";
                string posScript = "<script type='text/javascript'> function   setposition() { ";
                posScript += "document.all.divLoading.style.top=document.body.offsetHeight/2; ";
                posScript += "document.all.divLoading.style.left=document.body.offsetWidth/2; } ";
                posScript += "setposition();";
                posScript += "</script>";
                Response.Write(loadScript);
                Response.Write(posScript);
                Response.Flush();
            }
 	        base.OnPreInit(e);
        }

        /// <summary>
        /// page 初始化事件操作
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            MsLoginUser loginUser = HttpContext.Current.Session["LogUserInfo"] as MsLoginUser;
            if (null == loginUser || string.IsNullOrEmpty(loginUser.userId))
            {
                HttpContext.Current.Response.Write("<font color='red'>无效用户,请重新登陆!</font>");
                HttpContext.Current.Response.End();
            }

            /* valid operate menu */
            string curMenuId = (null != HttpContext.Current.Request.QueryString["TABID"]) ?
                    HttpContext.Current.Request.QueryString["TABID"].ToString() : "";
            if (!string.IsNullOrEmpty(curMenuId))
            {
                string operMenuIds = loginUser.operMenuIds;
                StringLib oString = new StringLib(operMenuIds);
                bool bContain = oString.Contains(curMenuId);
                if (!bContain)
                {
                    HttpContext.Current.Response.Write("<font color='red'>您无权访问该页面,请与管理员联系!</font>");
                    HttpContext.Current.Response.End();
                }
                oString = null;
            }

            loginUser = null;
        }
               
        /// <summary>
        /// page render 完成操作
        /// </summary>
        /// <param name="e"></param>
        protected override void  OnPreRenderComplete(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string strScript = "<script type='text/javascript'>";
                strScript += "document.getElementById('divLoading').style.display='none';";
                strScript += "</script>";
                Response.Write(strScript);
                Response.Flush();
            }

 	        base.OnPreRenderComplete(e);            
        }
               
    }
}
